package com.ruoyi.system.controller;

import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysLevel;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.parm.SysLevelParm;
import com.ruoyi.system.exception.GlobalSysException;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 【stop】Controller
 * 
 * @author ruoyi
 * @date 2024-03-31
 */
@Api(tags = "商家楼层")
@RestController
@RequestMapping("/bus/level")
public class BusLevelController extends BaseController
{
    @Autowired
    private ISysLevelService sysLevelService;
    @Autowired
    private ISysBusinessService sysBusinessService;
    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation(value = "商家楼层")
    @GetMapping("/list")
    public TableDataInfo list(SysLevel sysLevel) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        sysLevel.setBusinessId(bu.getId() );

        startPage();
        List<SysLevel> list = sysLevelService.selectSysLevelList(sysLevel);
        return getDataTable(list);
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation(value = "商家楼层详情")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id)
    {
        return success(sysLevelService.selectSysLevelById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation(value = "商家楼层添加")
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody @Valid SysLevelParm sysLevelParm)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }

        // 查询名称是否重复
        List<SysLevel> ret = sysLevelService.selectSysLevelByName(sysLevelParm.getName(), bu.getId(), sysLevelParm.getParkingId());

        if(ret.size()>0){
            throw new GlobalSysException("用户名称名称存在");
        }
        SysLevel sysLevel = new SysLevel();
        BeanUtils.copyProperties(sysLevelParm, sysLevel);
        sysLevel.setBusinessId(bu.getId() );
        sysLevel.setId(UUID.randomUUID().toString());
        return toAjax(sysLevelService.insertSysLevel(sysLevel));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "商家楼层修改")
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody @Valid SysLevelParm sysLevelParm)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }
        if(sysLevelParm.getId()==null){
            throw new GlobalSysException("id 不能为空");
        }

        SysLevel sysLevel = new SysLevel();
        BeanUtils.copyProperties(sysLevelParm, sysLevel);
        sysLevel.setBusinessId(bu.getId() );
        // 查询名称是否重复
        List<SysLevel> ret = sysLevelService.selectSysLevelByName(sysLevel.getName(), bu.getId(), sysLevel.getParkingId());
        if(ret.size()>0){
            for(SysLevel item : ret){
                if(item.getId().equals(sysLevel.getId())){
                    throw new GlobalSysException("名称已经存在");
                }
            }
        }

        return toAjax(sysLevelService.updateSysLevel(sysLevel));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "商家楼层删除")
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new GlobalSysException("用户未登陆");
        }

        return toAjax(sysLevelService.deleteSysLevelByIds(ids,bu.getId()));
    }
}
